package cc.sheji.controller;

import cc.sheji.Service.LoveService;
import cc.sheji.domain.*;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController

@RequestMapping(value = "/api/v1/love")
public class LoveController {

    @Autowired
    LoveService loveService;


    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(loveService.get(id));
    }

    @PostMapping("")
    public JsonResult create(@RequestBody @Validated() Love entity) {
        entity.setId(null);
        return JsonResult.Success(loveService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @RequestBody @Validated() Love entity) {
        entity.setId(id);
        return JsonResult.Success(loveService.update(entity));
    }

    @DeleteMapping("")
    public JsonResult delete( @RequestBody @Validated() Love entity) {
        System.out.println(entity);

        return JsonResult.Success(loveService.delete(entity));
    }

    @GetMapping()
    public JsonResult get(LoveQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        if(criteria.getQueryType()==1){
            criteria.setUserId(StpUtil.getLoginIdAsLong());
        }
        return JsonResult.Success(loveService.list(criteria,pageable));
    }

}
